<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://www.workerman.net/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */

/**
 * 用于检测业务代码死循环或者长时间阻塞等问题
 * 如果发现业务卡死，可以将下面declare打开（去掉//注释），并执行php start.php reload
 * 然后观察一段时间workerman.log看是否有process_timeout异常
 */
//declare(ticks=1);

use \GatewayWorker\Lib\Gateway;
use \GatewayWorker\Lib\Db;
use \Workerman\Lib\Timer;

/**
 * 主逻辑
 * 主要是处理 onConnect onMessage onClose 三个方法
 * onConnect 和 onClose 如果不需要可以不用实现并删除
 */
class Events
{
    /**
     * 当客户端连接时触发
     * 如果业务不需此回调可以删除onConnect
     * 
     * @param int $client_id 连接id
     */
    public static function onConnect($client_id)
    {
        //间隔60秒发送16进制命令 010300000002C40B
        $_SESSION['auth_timer_id'] = Timer::add(60, function ($client_id) {
            $sendStr = hex2bin('010300000002C40B');
            Gateway::sendToClient($client_id, $sendStr);
        }, array($client_id), true);

        // 向当前client_id发送数据 
        Gateway::sendToClient($client_id, $_SESSION['auth_timer_id']." $client_id\r\n");
        // 向所有人发送
//        Gateway::sendToAll("$client_id login\r\n");
    }
    
   /**
    * 当客户端发来消息时触发
    * @param int $client_id 连接id
    * @param mixed $message 具体消息
    */
   public static function onMessage($client_id, $message)
   {
       $str = Events::String2Hex($message);
       if (strlen($str) > 15) {
           $t = base_convert(substr($str, 6, 4), 16, 10);
           if ($t >= 61440) {
               $t = $t - 65536;
           }
           $temperature = $t / 10;
           $humidity = base_convert(substr($str, 10, 4), 16, 10) / 10;
           $db1 = Db::instance('db1');
           if ($temperature < 100 && $humidity < 100) {
               $insert_id = $db1->insert('fa_sensor_wenshilog')->cols(array('ws_id' => 1, 'temperature' => $temperature, 'humidity' => $humidity, 'updatetime' => time()))->query();
               return;
           }
       }

       // 打印结果
       Gateway::sendToClient($client_id, $_SERVER['REMOTE_ADDR'].'~'.$_SERVER['REMOTE_PORT'].'~'.$client_id.' ('.$str.')\r\n');

       //<----原代码
        // 向所有人发送 
//        Gateway::sendToAll("$client_id said $message\r\n");
   }

    public static function String2Hex($string)
    {
        $hex = '';
        for ($i = 0; $i < strlen($string); $i++) {
            $a = dechex(ord($string[$i]));
            if (strlen($a) == 1) {
                $a = '0' . $a;
            }
            $hex .= $a;
        }
        return $hex;
    }
   
   /**
    * 当用户断开连接时触发
    * @param int $client_id 连接id
    */
   public static function onClose($client_id)
   {
//       Timer::del($_SESSION['auth_timer_id']);
       // 向所有人发送 
//       GateWay::sendToAll("$client_id logout\r\n");
   }
}
